package com.cumt.threeway.view.dialog;

import android.app.Activity;
import android.support.v4.app.DialogFragment;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.cumt.threeway.R;
import com.cumt.threeway.activity.LoginActivity;
import com.cumt.threeway.util.ThreeWayUtil;


/**
 * Created by Jingjing.Hu on 2016/4/7.
 */
public class ForgetPwdDialog extends DialogFragment implements View.OnClickListener{

    private String mPhoneNum;
    private EditText mPhoneNumEditText;
    private Context mContext;
    private View mDelView;
    private View mNextStepView;

    public static ForgetPwdDialog newInstance() {
        return new ForgetPwdDialog();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int styleNormal = DialogFragment.STYLE_NORMAL;
        int theme = android.R.style.Theme_Light_NoTitleBar_Fullscreen;
        setStyle(styleNormal, theme);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mContext = activity;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_foeget_password_layout, null, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        mPhoneNumEditText = (EditText) view.findViewById(R.id.et_input_phone);
        mDelView = view.findViewById(R.id.btn_del);
        mNextStepView = view.findViewById(R.id.btn_next_step);
        view.findViewById(R.id.tv_back).setOnClickListener(this);
        view.findViewById(R.id.btn_del).setOnClickListener(this);
        mNextStepView.setOnClickListener(this);
        mNextStepView.setEnabled(false);
        mNextStepView.setAlpha(0.4f);
        mPhoneNumEditText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence charSequence, int arg1, int arg2, int arg3) {

            }

            @Override
            public void beforeTextChanged(CharSequence charSequence, int start, int count, int end) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (TextUtils.isEmpty(editable.toString())) {
                    mDelView.setVisibility(View.GONE);
                    mNextStepView.setAlpha(0.4f);
                    mNextStepView.setEnabled(false);
                } else {
                    mNextStepView.setEnabled(true);
                    mNextStepView.setAlpha(0.8f);
                    mDelView.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        if(v == null) {
            return;
        }
        switch (v.getId()) {
            case R.id.tv_back:
                dismiss();
                break;
            case R.id.btn_del:
                mPhoneNumEditText.setText("");
                break;
            case R.id.btn_next_step:
                mPhoneNum = mPhoneNumEditText.getText().toString();
                if(ThreeWayUtil.getInstance().isPhoneNum(mPhoneNum)) {
                    ThreeWayUtil.getInstance().showToast(mContext,mContext.getString(R.string.phone_format_error));
                } else if (mContext instanceof LoginActivity){
                    ((LoginActivity)mContext).goCheckCodeDialog(mPhoneNum);
                }
                break;
        }
    }
}
